var app = angular.module('ZJPC');
app.controller('ReCCarFileCtrl', ['$scope','$state','$rootScope','$stateParams','DataService','$filter',ReCCarFileCtrl]);

function ReCCarFileCtrl($scope,$state,$rootScope,$stateParams,DataService,$filter) {
	
	$scope.carFile = {name:$stateParams.name}

	$scope.pageOption = {
		pageSize:10,
		itemCount:4
	}
	
	$scope.searchTrack = function(){
		DataService.getCarfellowcar($filter('date')($scope.starttime,'yyyy-MM-dd HH:mm:ss'),$filter('date')($scope.endtime,'yyyy-MM-dd HH:mm:ss'),$scope.former,$scope.after,$stateParams.name,function(data){
			$scope.listsearchList = data;
			$scope.pageOption.current = 0;
			$scope.pageOption = {
				pageSize:10,
				itemCount:$scope.listsearchList.length
			}
		})
	}

	$scope.getData = function(item){
		$scope.list = $scope.listsearchList.slice(item[0]-1,item[1])
	}


	$scope.former = 50;

	$scope.after = 50;

	$scope.endtime = new Date();
	$scope.starttime = new Date().setHours(0,0,0);
	$scope.searchTrack();

	$scope.baseInfo = function(id){
		console.log(id)
		var url = $state.href('app.file.carfile.basic',{name:id});
	  	window.open(url,'_blank');
	}

		/*时间控件校验相关设置*/
	$scope.endDateBeforeRender = endDateBeforeRender
	$scope.endDateOnSetTime = endDateOnSetTime
	$scope.startDateBeforeRender = startDateBeforeRender
	$scope.startDateOnSetTime = startDateOnSetTime

	function startDateOnSetTime () {
	  $scope.$broadcast('start-date-changed');
	}

	function endDateOnSetTime () {
	  $scope.$broadcast('end-date-changed');
	}

	function startDateBeforeRender ($dates) {
	  if ($scope.endtime) {
	    $scope.endtime = new Date($scope.endtime).setHours(23,59,59)
	    var activeDate = moment($scope.endtime);

	    $dates.filter(function (date) {
	      return date.localDateValue() >= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}

	function endDateBeforeRender ($view, $dates) {
	  if ($scope.starttime) {
	    var activeDate = moment($scope.starttime).subtract(1, $view).add(1, 'minute');

	    $dates.filter(function (date) {
	      return date.localDateValue() <= activeDate.valueOf()
	    }).forEach(function (date) {
	      date.selectable = false;
	    })
	  }
	}


}